package com.b2b.controller;

import com.b2b.exception.CustomException;
import com.b2b.model.QuotationListDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.InquiryQuotation;
import com.b2b.model.entity.QuotationGoods;
import com.b2b.model.entity.QuotationRecord;
import com.b2b.model.receive.CompanyId;
import com.b2b.model.receive.QuotationFromI;
import com.b2b.model.receive.QuotationFromQ;
import com.b2b.util.InquiryQuotationUtil;
import com.b2b.util.QuotationGoodsUtil;
import com.b2b.util.QuotationUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/quotation")
@PropertySource("classpath:config.properties")
public class QuotationController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final QuotationUtil quotationUtil;
    private final InquiryQuotationUtil inquiryQuotationUtil;
    private final QuotationGoodsUtil quotationGoodsUtil;
    @Autowired
    public QuotationController(QuotationUtil quotationUtil,InquiryQuotationUtil inquiryQuotationUtil,QuotationGoodsUtil quotationGoodsUtil){
        this.quotationUtil = quotationUtil;
        this.inquiryQuotationUtil = inquiryQuotationUtil;
        this.quotationGoodsUtil = quotationGoodsUtil;
    }
    @PostMapping
    public ResponseBean add(@RequestBody QuotationListDto quotationListDto){
        Integer quotationId = quotationUtil.insertQuotationRecord(quotationListDto);
        if (quotationId<=0){
            throw new CustomException("QuotationRecord 插入失败(Query Failure)");
        }
        quotationListDto.setQuotationId(quotationId);
        Integer count = inquiryQuotationUtil.insertInquiryQuotation(quotationListDto);
        if (count<=0){
            throw new CustomException("InquiryQuotation 插入失败(Query Failure)");
        }
        Integer quotationGoods = quotationGoodsUtil.insertQuotationGoods(quotationListDto);
        if (quotationGoods<=0){
            throw new CustomException("InquiryQuotation 插入失败(Query Failure)");
        }
        InquiryQuotation inquiryQuotation = new InquiryQuotation();
        inquiryQuotation.setQuotationId(quotationId);
        inquiryQuotation.setInquiryId(quotationListDto.getInquiryId());
        return new ResponseBean(HttpStatus.OK.value(), "报价成功", inquiryQuotation);
    }



    @GetMapping("/quotationRecord")
    public ResponseBean getQuotationRecord(@RequestBody InquiryQuotation inquiryQuotation){
        Integer quotationId = inquiryQuotation.getQuotationId();

        QuotationRecord quotationRecord = quotationUtil.selectQuotationRecord(quotationId);

        return new ResponseBean(HttpStatus.OK.value(),"查询成功(Select Success)",quotationRecord);
    }

    @GetMapping("/launchTry")
    public ResponseBean getQuotationByCFromI(QuotationRecord quotationRecord){
        Integer companyId = quotationRecord.getCompanyId();
        List<QuotationFromI> quotationFromIS = quotationUtil.selectByLaunchIId(companyId);
        return new ResponseBean(HttpStatus.OK.value(),"查询成功(Select Success)",quotationFromIS);
    }
    @GetMapping("/launchI")
    public ResponseBean tryQuotationByCFromI(CompanyId companyDto){
        Integer companyId = companyDto.getCompanyId();
        if (companyDto.getPage() == null){
            companyDto.setPage(1);
        }
        if (companyDto.getRows() == null){
            companyDto.setRows(10);
        }
        PageHelper.startPage(companyDto.getPage(), companyDto.getRows());
        List<QuotationFromI> quotationFromIS = quotationUtil.selectByLaunchIId(companyId);
        PageInfo<QuotationFromI> selectPage = new PageInfo<QuotationFromI>(quotationFromIS);
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("quotationList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(),"查询成功(Select Success)",result);
    }
    @GetMapping("/launchQ")
    public ResponseBean getQuotationByCFromQ(CompanyId companyIdDto){
        Integer companyId = companyIdDto.getCompanyId();
        if (companyId ==null){
            throw new CustomException("InquiryQuotation 查询失败(Select Failure)");
        }
        PageHelper.startPage(companyIdDto.getPage(), companyIdDto.getRows());
        List<QuotationFromQ> quotationFromQS = quotationUtil.selectByLaunchQId(companyId);
        PageInfo<QuotationFromQ> selectPage = new PageInfo<QuotationFromQ>(quotationFromQS);
        if (quotationFromQS == null || quotationFromQS.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("quotationList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(),"查询成功(Select Success)",result);
    }
    @GetMapping("/byInquiryId")
    public ResponseBean getQuotationRecordByI(InquiryQuotation inquiryQuotation){
        Integer inquiryId = inquiryQuotation.getInquiryId();
        List<QuotationRecord> quotationRecordList = quotationUtil.selectByInquiryId(inquiryId);
        return new ResponseBean(HttpStatus.OK.value(),"查询成功(Select Success)",quotationRecordList);
    }

    @GetMapping("/goods")
    public ResponseBean getQuotationGoods(@RequestBody InquiryQuotation inquiryQuotation){
        Integer quotationId = inquiryQuotation.getQuotationId();

        List<QuotationGoods> quotationGoodsList = quotationGoodsUtil.selectGoods(quotationId);

        return new ResponseBean(HttpStatus.OK.value(),"查询成功(Select Success)",quotationGoodsList);
    }


    @DeleteMapping("/quotation")
    public ResponseBean deleteQuotation(@RequestBody InquiryQuotation inquiryQuotation){
        Integer quotationId = inquiryQuotation.getQuotationId();

        Integer result = quotationUtil.deleteQuotation(quotationId);
        if (result<=0){
            throw new CustomException("删除失败(Delete Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(),"删除成功(Delete Success)",null);
    }

    @DeleteMapping("/goods")
    public ResponseBean deleteGoods(@RequestBody QuotationGoods quotationGoods){
        Integer result = quotationGoodsUtil.deleteGood(quotationGoods);
        if (result<=0){
            throw new CustomException("删除失败(Delete Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(),"删除成功(Delete Success)",null);
    }


    @PutMapping("/quotation")
    public ResponseBean updateQuotation(@RequestBody QuotationRecord quotationRecord){
        Integer result = quotationUtil.updateQuotation(quotationRecord);
        if (result<=0){
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(),"更新成功(Update Success)",null);
    }

    @PutMapping("/goods")
    public ResponseBean updateGoods(@RequestBody QuotationGoods quotationGoods){
        Integer result = quotationGoodsUtil.updateGood(quotationGoods);
        if (result<=0){
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(),"更新成功(Update Success)",null);
    }
}
